package com.wcl.hencodertest.test07_drawing;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.wcl.hencodertest.Util;

/**
 * @项目名： HenCoderTest
 * @包名： com.wcl.hencodertest.test07_drawing
 * @文件名: ClipVeiw
 * @创建者: kilin
 * @创建时间: 2020/4/17 19:16
 * @描述： TODO
 */
public class ClipVeiw extends View {

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public static final int BITMAP_WIDTH = ConvertUtils.dp2px(300);
    public static final int RADIUS = ConvertUtils.dp2px(100);

    public ClipVeiw(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    Path path = new Path();

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        path.addCircle(getWidth() / 2F, getHeight() / 2F, RADIUS, Path.Direction.CW);
        canvas.clipPath(path);
        canvas.drawBitmap(Util.getAvatar(BITMAP_WIDTH),
                getWidth() / 2F - BITMAP_WIDTH / 2F,
                getHeight() / 2F - BITMAP_WIDTH / 2F, mPaint);
        canvas.restore();

        canvas.drawBitmap(Util.getAvatar(BITMAP_WIDTH),
                getWidth() / 2F,
                getHeight() / 2F, mPaint);
    }
}
